<?php
/**
 * Created by PhpStorm.
 * User: 小蛮哼哼哼
 * Email: 243194993@qq.com
 * Date: 2021/11/17
 * Time: 21:38
 * motto: 现在的努力是为了小时候吹过的牛逼！
 */

declare(strict_types=1);

namespace app\admin\controller;

use app\common\business\MoneyList as BusObj;
use app\common\lib\Show;
use app\common\lib\Status;
use app\common\lib\Time;
use think\App;

class Withdrawal extends Base
{
    protected BusObj $busObj;
    protected $indexWhere = [];

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->busObj = new BusObj();
    }
    public function index()
    {
        $relname = $this->request->param('relname','','trim');
        $reCode = $this->request->param('reCode','','trim');
        $classify = $this->request->param('classify','','trim');
        $map = [];
        if(!empty($classify)){
            $time = explode('~',$classify);
            $map[] = [
                ['create_time','between',[
                    strtotime($time[0]),
                    strtotime($time[1]),
                ]]
            ];
        }else{
            $map[] = [
                ['create_time','between',Time::month()]
            ];
        }
        if(!empty($relname)){
            $map[] = [
                ['relname','=',$relname]
            ];
        }
        if(!empty($reCode)){
            $map[] = [
                ['reCode','=',$reCode]
            ];
        }
        $this->indexWhere = $map;
        return parent::index(); // TODO: Change the autogenerated stub
    }

    /**
     * @return \think\response\Json
     */
    public function openState()
    {
        $id = $this->request->param('id', 0, 'intval');
        if ($id == 0) {
            return Show::json_show(Status::status_error, "内部错误");
        }
        try {
            $result = $this->busObj->updateByData($id, [
                "state" => 1
            ]);
        } catch (\Exception $e) {
            return Show::json_show(Status::status_error, $e->getMessage());
        }
        if ($result) {
            return Show::json_show(Status::status_normal, "操作成功");
        } else {
            return Show::json_show(Status::status_normal, "操作失败");
        }
    }

    public function openError()
    {
        $id = $this->request->param('id', 0, 'intval');
        $text = $this->request->param('text', '', 'trim');
        if (empty($text)) {
            return Show::json_show(Status::status_error, "请输入拒绝原因");
        }
        if ($id == 0) {
            return Show::json_show(Status::status_error, "内部错误");
        }
        try {
            $result = $this->busObj->updateByData($id, [
                "state" => 2,
                "state_msg" => $text
            ]);
        } catch (\Exception $e) {
            return Show::json_show(Status::status_error, $e->getMessage());
        }
        if ($result) {
            return Show::json_show(Status::status_normal, "操作成功");
        } else {
            return Show::json_show(Status::status_normal, "操作失败");
        }
    }
}